package Transactions;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public abstract class DB {

	/*
	 * Husk er skifte dbname, username og password :)
	 */
	public static Connection conn = null;
	private static String url = "jdbc:mysql://localhost:3306/"; // url
	private static String dbName = "chat"; // database name
	private static String userName = "root"; // db username
	private static String password = "king1978"; // db password
	private static String driver = "com.mysql.jdbc.Driver"; // driver to use (mysql)

	/**
	 * Method to open database connection
	 */
	public static Connection open() {
		if (conn == null) {
			try {
				Class.forName(driver).newInstance();
				DB.conn = DriverManager.getConnection(url + dbName, userName,
						password);
				return conn;
			} catch (Exception e) {
				return null;
			}
		} 
		else {
			return conn;
		}
	}

	/**
	 * Method to close database connection
	 */
	public static void close() {
		try {
			DB.conn.close();
			DB.conn = null;
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}


